//
//  XFStatusPictureView.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/21.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

class XFStatusPictureView: UICollectionView {
    
    //MARK: - 属性
    
    private let ReuseIdentifier = "StatusPictureViewReuseIdentifier"

    var status: XFStatus? {
        didSet{
            self.reloadData()
        }
    }
    
    
    //计算视图的size
    func calculateViewSize() -> CGSize {
        
        let itemSize = CGSize(width: 90, height: 90)
        
        let margin :CGFloat = 10
        
        layout.itemSize = itemSize
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        let count = status?.pictureURLs?.count ?? 0
        
        if count == 0 {
            return CGSizeZero
        }
        
        if count == 1 {
            
            let size = CGSize(width: 150, height: 120)
            layout.itemSize = size
            return size
        }
        
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
        
        if count == 4 {
            let value = 2 * itemSize.width + margin
            
            return CGSize(width: value, height: value)
        }
        
        let colum = 3
        
        let width = CGFloat(colum) * itemSize.width + (CGFloat(colum) - 1) * margin
        
        let row = (count + colum - 1) / colum
        
        let height = CGFloat(row) * itemSize.height + (CGFloat(row) - 1) * margin
        
        return CGSize(width: width, height: height)
        
    }
    
    
 
    private var layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init () {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        backgroundColor = UIColor.clearColor()
        
        self.registerClass(XFStatusPictureViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        
        self.dataSource = self
    }

}

extension XFStatusPictureView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return status?.pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! XFStatusPictureViewCell
        
        cell.imageURL = status?.pictureURLs?[indexPath.item]
        
        return cell
    }
    
}


//自定义cell
class XFStatusPictureViewCell: UICollectionViewCell {
    
    
    var imageURL:NSURL? {
        didSet {
            imageView.sd_setImageWithURL(imageURL)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }

    //准备UI
    private func prepareUI() {
        
        contentView.addSubview(imageView)
        
        imageView.ff_Fill(contentView)
    }
    
    //MARK: - 懒加载控件
    private lazy var imageView: UIImageView = UIImageView(image: nil)
    
}


